package com.cj.mobile.iabp.mvp.ui.activity;

import static com.jess.arms.utils.Preconditions.checkNotNull;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cj.mobile.iabp.R;
import com.cj.mobile.iabp.app.global.Constant;
import com.cj.mobile.iabp.app.utils.CommonUtils;
import com.cj.mobile.iabp.di.component.DaggerSurgeryInfoComponent;
import com.cj.mobile.iabp.mvp.contract.SurgeryInfoContract;
import com.cj.mobile.iabp.mvp.model.entity.SurgeryInfo;
import com.cj.mobile.iabp.mvp.presenter.SurgeryInfoPresenter;
import com.cj.mobile.iabp.mvp.ui.adapter.SignInImgListAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created on 2025/10/01 23:40
 * 查看手术信息
 *
 * @author Love_xie
 * module name is SurgeryInfoActivity
 */
public class SurgeryInfoActivity extends BaseActivity<SurgeryInfoPresenter> implements SurgeryInfoContract.View {
    /*----------------------------------------------------------------控件信息----------------------------------------------------------------*/
    @BindView(R.id.txvi_surgeryinfo_code)
    TextView txviCode;                                                                              // 登记号
    @BindView(R.id.txvi_surgeryinfo_name)
    TextView txviName;                                                                              // 患者姓名

    @BindView(R.id.recy_surgeryinfo_images)
    RecyclerView reviImgList;                                                                       // 纸质单据
    @BindView(R.id.txvi_surgeryinfo_surgeryName)
    TextView surgeryName;                                                                           // 手术名称
    @BindView(R.id.txvi_surgeryinfo_surgeryTime)
    TextView surgeryTime;                                                                           // 手术时间
    @BindView(R.id.txvi_surgeryinfo_installTime)
    TextView installTime;                                                                           // 安装时间
    @BindView(R.id.txvi_surgeryinfo_restTime)
    TextView restTime;                                                                              // 是否工作日
    @BindView(R.id.txvi_surgeryinfo_nyha)
    TextView nyha;                                                                                  // 术前心功能NYHA
    @BindView(R.id.txvi_surgeryinfo_bloodScore)
    TextView bloodScore;                                                                            // 射血分数
    @BindView(R.id.txvi_surgeryinfo_leftWidth)
    TextView leftWidth;                                                                              // 左室舒张末期内径
    @BindView(R.id.txvi_surgeryinfo_isOff)
    TextView isOff;                                                                                 // 是否内膜剥脱
    @BindView(R.id.txvi_surgeryinfo_cpbMinits)
    TextView cpbMinits;                                                                              // CPB时间
    @BindView(R.id.txvi_surgeryinfo_stopNinits)
    TextView stopNinits;                                                                              // 阻断时间
    @BindView(R.id.txvi_surgeryinfo_cagb)
    TextView cagb;                                                                                  // CABG支数
    @BindView(R.id.txvi_surgeryinfo_placeId)
    TextView placeId;                                                                               // 冠状动脉病变部位
    @BindView(R.id.txvi_surgeryinfo_ballType)
    TextView ballType;                                                                              // 球囊类型
    @BindView(R.id.txvi_surgeryinfo_ballWidth)
    TextView ballWidth;                                                                             // 球囊内径
    @BindView(R.id.txvi_surgeryinfo_ballSize)
    TextView ballSize;                                                                              // 球囊容积
    @BindView(R.id.txvi_surgeryinfo_place)
    TextView place;                                                                                 // 置入位置
    @BindView(R.id.txvi_surgeryinfo_isDmc)
    TextView isDmc;                                                                                 // 是否动脉鞘
    @BindView(R.id.txvi_surgeryinfo_rightRoldWidth)
    TextView rightRoldWidth;                                                                        // 右侧股动脉内径
    @BindView(R.id.txvi_surgeryinfo_rightRoldHeight)
    TextView rightRoldHeight;                                                                       // 右侧股动脉狭窄程度
    @BindView(R.id.txvi_surgeryinfo_rightRoldHasColor)
    TextView rightRoldHasColor;                                                                     // 右侧股动脉斑块
    @BindView(R.id.txvi_surgeryinfo_leftRoldWidth)
    TextView leftRoldWidth;                                                                         // 左侧股动脉内径
    @BindView(R.id.txvi_surgeryinfo_leftRoldHeight)
    TextView leftRoldHeight;                                                                        // 左侧股动脉狭窄程度
    @BindView(R.id.txvi_surgeryinfo_leftRoldHasColor)
    TextView leftRoldHasColor;                                                                      // 左侧股动脉斑块
    @BindView(R.id.txvi_surgeryinfo_installWhen)
    TextView installWhen;                                                                           // 安装时机
    @BindView(R.id.txvi_surgeryinfo_installFor)
    TextView installFor;                                                                            // 安装时状态
    @BindView(R.id.txvi_surgeryinfo_ecmoPlace)
    TextView ecmoPlace;                                                                             // 联合ECMO
    @BindView(R.id.txvi_surgeryinfo_installPlace)
    TextView installPlace;                                                                          // 安装地点
    @BindView(R.id.txvi_surgeryinfo_installWhy)
    TextView installWhy;                                                                            // 安装时应用原因、患者及安装情况
    @BindView(R.id.txvi_surgeryinfo_beforeXinlv)
    TextView beforeXinlv;                                                                           // 安装前心律
    @BindView(R.id.txvi_surgeryinfo_beforeXinlvSize)
    TextView beforeXinlvSize;                                                                       // 安装前心率
    @BindView(R.id.txvi_surgeryinfo_beforeBloodPasur)
    TextView beforeBloodPasur;                                                                      // 安装前血压
    @BindView(R.id.txvi_surgeryinfo_afterXinlv)
    TextView afterXinlv;                                                                            // 安装后心律
    @BindView(R.id.txvi_surgeryinfo_afterXinlvSize)
    TextView afterXinlvSize;                                                                        // 安装后心率
    @BindView(R.id.txvi_surgeryinfo_afterBloodPasur)
    TextView afterBloodPasur;                                                                       // 安装后血压
    @BindView(R.id.txvi_surgeryinfo_beforeDba)
    TextView beforeDba;                                                                             // 安装前多巴胺药物量
    @BindView(R.id.txvi_surgeryinfo_beforeSsx)
    TextView beforeSsx;                                                                             // 安装前肾上腺素药物量
    @BindView(R.id.txvi_surgeryinfo_beforeQjssx)
    TextView beforeQjssx;                                                                           // 安装前去甲肾上腺素药物量
    @BindView(R.id.txvi_surgeryinfo_beforeMln)
    TextView beforeMln;                                                                              // 安装前米力农药物量
    @BindView(R.id.txvi_surgeryinfo_beforeCt)
    TextView beforeCt;                                                                              // 安装前垂体药物量
    @BindView(R.id.txvi_surgeryinfo_beforeDbf)
    TextView beforeDbf;                                                                             // 安装前多巴酚丁胺药物量
    @BindView(R.id.txvi_surgeryinfo_beforeSys)
    TextView beforeSys;                                                                              // 安装前血管升压素
    @BindView(R.id.txvi_surgeryinfo_beforeVis)
    TextView beforeVis;                                                                              // VIS
    @BindView(R.id.txvi_surgeryinfo_afterDba)
    TextView afterDba;                                                                              // 安装后多巴胺药物量
    @BindView(R.id.txvi_surgeryinfo_afterSsx)
    TextView afterSsx;                                                                              // 安装后肾上腺素药物量
    @BindView(R.id.txvi_surgeryinfo_afterQjssx)
    TextView afterQjssx;                                                                              // 安装后去甲肾上腺素药物量
    @BindView(R.id.txvi_surgeryinfo_afterMln)
    TextView afterMln;                                                                              // 安装后米力农药物量
    @BindView(R.id.txvi_surgeryinfo_afterCt)
    TextView afterCt;                                                                              // 安装后垂体药物量
    @BindView(R.id.txvi_surgeryinfo_afterDbf)
    TextView afterDbf;                                                                              // 安装后多巴酚丁胺药物量
    @BindView(R.id.txvi_surgeryinfo_afterSys)
    TextView afterSys;                                                                              // 安装后血管升压素
    @BindView(R.id.txvi_surgeryinfo_afterVis)
    TextView afterVis;                                                                              // VIS
    @BindView(R.id.txvi_surgeryinfo_problemA)
    TextView problemA;                                                                              // 反搏效果和反搏过程中出现的问题
    @BindView(R.id.txvi_surgeryinfo_machineCode)
    TextView machineCode;                                                                              // 机器代码
    @BindView(R.id.txvi_surgeryinfo_iabpAst)
    TextView iabpAst;                                                                               // AST
    @BindView(R.id.txvi_surgeryinfo_iabpAlt)
    TextView iabpAlt;                                                                               // ALT
    @BindView(R.id.txvi_surgeryinfo_iabpCr)
    TextView iabpCr;                                                                                // 肌酐Cr
    @BindView(R.id.txvi_surgeryinfo_iabpEgfr)
    TextView iabpEgfr;                                                                              // eGFR
    @BindView(R.id.txvi_surgeryinfo_iabpMyo)
    TextView iabpMyo;                                                                               // 肌红蛋白
    @BindView(R.id.txvi_surgeryinfo_iabpClac)
    TextView iabpClac;                                                                              // 乳酸cLac
    @BindView(R.id.txvi_surgeryinfo_iabpGa)
    TextView iabpGa;                                                                                // 肌钙蛋白
    @BindView(R.id.txvi_surgeryinfo_downSpecial)
    TextView downSpecial;                                                                           // 下肢动脉超声特殊
    @BindView(R.id.txvi_surgeryinfo_downBeforeA)
    TextView downBeforeA;                                                                           // 安装前流速
    @BindView(R.id.txvi_surgeryinfo_downBeforeB)
    TextView downBeforeB;                                                                           // 股总
    @BindView(R.id.txvi_surgeryinfo_downBeforeC)
    TextView downBeforeC;                                                                           // 股浅
    @BindView(R.id.txvi_surgeryinfo_downBeforeD)
    TextView downBeforeD;                                                                           // 股深
    @BindView(R.id.txvi_surgeryinfo_downBeforeE)
    TextView downBeforeE;                                                                           // 胭
    @BindView(R.id.txvi_surgeryinfo_downBeforeF)
    TextView downBeforeF;                                                                           // 胫前
    @BindView(R.id.txvi_surgeryinfo_downBeforeG)
    TextView downBeforeG;                                                                           // 胫后
    @BindView(R.id.txvi_surgeryinfo_downBeforeH)
    TextView downBeforeH;                                                                           // 足背
    @BindView(R.id.txvi_surgeryinfo_downAfterA)
    TextView downAfterA;                                                                            // 安装后流速
    @BindView(R.id.txvi_surgeryinfo_downAfterB)
    TextView downAfterB;                                                                            // 股总
    @BindView(R.id.txvi_surgeryinfo_downAfterC)
    TextView downAfterC;                                                                            // 股浅
    @BindView(R.id.txvi_surgeryinfo_downAfterD)
    TextView downAfterD;                                                                            // 股深
    @BindView(R.id.txvi_surgeryinfo_downAfterE)
    TextView downAfterE;                                                                            // 胭
    @BindView(R.id.txvi_surgeryinfo_downAfterF)
    TextView downAfterF;                                                                            // 胫前
    @BindView(R.id.txvi_surgeryinfo_downAfterG)
    TextView downAfterG;                                                                            // 胫后
    @BindView(R.id.txvi_surgeryinfo_downAfterH)
    TextView downAfterH;                                                                            // 足背
    /*----------------------------------------------------------------业务信息----------------------------------------------------------------*/
    // 数据源
    private SurgeryInfo info;

    // 患者姓名
    private String name;
    // 登记号
    private String code;

    @Inject
    GridLayoutManager mGridLayoutManager;
    @Inject
    List<String> mRecyImgList;
    @Inject
    SignInImgListAdapter mImgAdapter;

    /**
     * 根据主题使用不同的颜色。
     * 如果想要纯透明，则需要重写此方法，返回值为 -1 即可。
     */
    public int useStatusBarColor() {
        TypedValue typedValue = new TypedValue();
        getTheme().resolveAttribute(android.R.color.white, typedValue, true);
        return typedValue.data;
    }

    /**
     * 将状态栏改为浅色模式(状态栏 icon 和字体会变成深色)
     */
    public boolean useLightStatusBar() {
        return true;
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerSurgeryInfoComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_surgery_info;
    }

    public void getBundleValues(Bundle bundle) {
        if (bundle != null) {
            info = bundle.getParcelable("info");
            code = bundle.getString("code");
            name = bundle.getString("name");
        }
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        setTitle("手术信息");

        getBundleValues(getIntent().getExtras());
        initRecyclerView();

        loadData();
    }

    private void initRecyclerView() {
        ArmsUtils.configRecyclerView(reviImgList, mGridLayoutManager);
        reviImgList.setAdapter(mImgAdapter);
        mImgAdapter.setOnItemClickListener((view, viewType, data, position) -> {
            String val = (String) data;
            showImage(val);
        });
    }

    /**
     * 显示图片
     */
    public void showImage(String image) {
        if (!TextUtils.isEmpty(image)) {
            Intent intent = new Intent(this, PhotoViewActivity.class);
            intent.putExtra(Constant.IMAGE_URL, image);
            startActivity(intent);
        }
    }

    private void loadData() {
        if (info != null) {
            txviCode.setText(CommonUtils.isEmptyReturnStr(code, "暂无"));
            txviName.setText(CommonUtils.isEmptyReturnStr(name));

            if (!TextUtils.isEmpty(info.getImages())) {
                String[] path = info.getImages().split(",");
                mRecyImgList.clear();
                Collections.addAll(mRecyImgList, path);
                mImgAdapter.notifyDataSetChanged();
            }

            if (!TextUtils.isEmpty(info.getSurgeryName())) {
                // 特殊处理：去掉“其他”和特殊字符
                String result = CommonUtils.dataProcessing(
                        info.getSurgeryName().split(","),
                        "其他(详述)");

                surgeryName.setText(CommonUtils.isEmptyReturnStr(result));
            }

            surgeryTime.setText(CommonUtils.isEmptyReturnStr(info.getSurgeryTime()));
            installTime.setText(CommonUtils.isEmptyReturnStr(info.getInstallTime()));
            restTime.setText(CommonUtils.isEmptyReturnStr(info.getRestTime()));
            nyha.setText(CommonUtils.isEmptyReturnStr(info.getNyha()));
            bloodScore.setText(CommonUtils.isEmptyReturnStr(info.getBloodScore()));
            leftWidth.setText(CommonUtils.isEmptyReturnStr(info.getLeftWidth()));
            isOff.setText(CommonUtils.isEmptyReturnStr(info.getIsOff()));
            cpbMinits.setText(CommonUtils.isEmptyReturnStr(info.getCpbMinits()));
            stopNinits.setText(CommonUtils.isEmptyReturnStr(info.getStopNinits()));
            cagb.setText(CommonUtils.isEmptyReturnStr(info.getCagb()));
            placeId.setText(CommonUtils.isEmptyReturnStr(info.getPlaceId()));
            ballType.setText(CommonUtils.isEmptyReturnStr(info.getBallType()));
            ballWidth.setText(CommonUtils.isEmptyReturnStr(info.getBallWidth()));
            ballSize.setText(CommonUtils.isEmptyReturnStr(info.getBallSize()));
            place.setText(CommonUtils.isEmptyReturnStr(info.getPlace()));
            isDmc.setText(CommonUtils.isEmptyReturnStr(info.getIsDmc()));
            rightRoldWidth.setText(CommonUtils.isEmptyReturnStr(info.getRightRoldWidth()));
            rightRoldHeight.setText(CommonUtils.isEmptyReturnStr(info.getRightRoldHeight()));
            rightRoldHasColor.setText(CommonUtils.isEmptyReturnStr(info.getRightRoldHasColor()));
            leftRoldWidth.setText(CommonUtils.isEmptyReturnStr(info.getLeftRoldWidth()));
            leftRoldHeight.setText(CommonUtils.isEmptyReturnStr(info.getLeftRoldHeight()));
            leftRoldHasColor.setText(CommonUtils.isEmptyReturnStr(info.getLeftRoldHasColor()));
            installWhen.setText(CommonUtils.isEmptyReturnStr(info.getInstallWhen()));
            installFor.setText(CommonUtils.isEmptyReturnStr(info.getInstallFor()));
            ecmoPlace.setText(CommonUtils.isEmptyReturnStr(info.getEcmoPlace()));
            installPlace.setText(CommonUtils.isEmptyReturnStr(info.getInstallPlace()));
            installWhy.setText(CommonUtils.isEmptyReturnStr(info.getInstallWhy()));

            if (!TextUtils.isEmpty(info.getBeforeXinlv())) {
                if (info.getBeforeXinlv().contains("@_")) {
                    String[] values = info.getBeforeXinlv().split(",");
                    for (String item : values) {
                        if (item.contains("@_")) {
                            beforeXinlv.setText(item.replace("@_", ""));
                        }
                    }
                } else {
                    beforeXinlv.setText(info.getBeforeXinlv());
                }
            }

            beforeXinlvSize.setText(CommonUtils.isEmptyReturnStr(info.getBeforeXinlvSize()));
            beforeBloodPasur.setText(CommonUtils.isEmptyReturnStr(info.getBeforeBloodPasur()));

            if (!TextUtils.isEmpty(info.getAfterXinlv())) {
                if (info.getAfterXinlv().contains("@_")) {
                    String[] values = info.getAfterXinlv().split(",");
                    for (String item : values) {
                        if (item.contains("@_")) {
                            afterXinlv.setText(item.replace("@_", ""));
                        }
                    }
                } else {
                    afterXinlv.setText(info.getAfterXinlv());
                }
            }

            afterXinlvSize.setText(CommonUtils.isEmptyReturnStr(info.getAfterXinlvSize()));
            afterBloodPasur.setText(CommonUtils.isEmptyReturnStr(info.getAfterBloodPasur()));
            beforeDba.setText(CommonUtils.isEmptyReturnStr(info.getBeforeDba()));
            beforeSsx.setText(CommonUtils.isEmptyReturnStr(info.getBeforeSsx()));
            beforeQjssx.setText(CommonUtils.isEmptyReturnStr(info.getBeforeQjssx()));
            beforeMln.setText(CommonUtils.isEmptyReturnStr(info.getBeforeMln()));
            beforeCt.setText(CommonUtils.isEmptyReturnStr(info.getBeforeCt()));
            beforeDbf.setText(CommonUtils.isEmptyReturnStr(info.getBeforeDbf()));
            beforeSys.setText(CommonUtils.isEmptyReturnStr(info.getBeforeSys()));
            beforeVis.setText(CommonUtils.isEmptyReturnStr(info.getBeforeVis()));
            afterDba.setText(CommonUtils.isEmptyReturnStr(info.getAfterDba()));
            afterSsx.setText(CommonUtils.isEmptyReturnStr(info.getAfterSsx()));
            afterQjssx.setText(CommonUtils.isEmptyReturnStr(info.getAfterQjssx()));
            afterMln.setText(CommonUtils.isEmptyReturnStr(info.getAfterMln()));
            afterCt.setText(CommonUtils.isEmptyReturnStr(info.getAfterCt()));
            afterDbf.setText(CommonUtils.isEmptyReturnStr(info.getAfterDbf()));
            afterSys.setText(CommonUtils.isEmptyReturnStr(info.getAfterSys()));
            afterVis.setText(CommonUtils.isEmptyReturnStr(info.getAfterVis()));
            problemA.setText(CommonUtils.isEmptyReturnStr(info.getProblemA()));
            machineCode.setText(CommonUtils.isEmptyReturnStr(info.getMachineCode()));
            iabpAst.setText(CommonUtils.isEmptyReturnStr(info.getIabpAst()));
            iabpAlt.setText(CommonUtils.isEmptyReturnStr(info.getIabpAlt()));
            iabpCr.setText(CommonUtils.isEmptyReturnStr(info.getIabpCr()));
            iabpEgfr.setText(CommonUtils.isEmptyReturnStr(info.getIabpEgfr()));
            iabpMyo.setText(CommonUtils.isEmptyReturnStr(info.getIabpMyo()));
            iabpClac.setText(CommonUtils.isEmptyReturnStr(info.getIabpClac()));
            iabpGa.setText(CommonUtils.isEmptyReturnStr(info.getIabpGa()));
            downSpecial.setText(CommonUtils.isEmptyReturnStr(info.getDownSpecial()));
            downBeforeA.setText(CommonUtils.isEmptyReturnStr(info.getDownBeforeA()));
            downBeforeB.setText(CommonUtils.isEmptyReturnStr(info.getDownBeforeB()));
            downBeforeC.setText(CommonUtils.isEmptyReturnStr(info.getDownBeforeC()));
            downBeforeD.setText(CommonUtils.isEmptyReturnStr(info.getDownBeforeD()));
            downBeforeE.setText(CommonUtils.isEmptyReturnStr(info.getDownBeforeE()));
            downBeforeF.setText(CommonUtils.isEmptyReturnStr(info.getDownBeforeF()));
            downBeforeG.setText(CommonUtils.isEmptyReturnStr(info.getDownBeforeG()));
            downBeforeH.setText(CommonUtils.isEmptyReturnStr(info.getDownBeforeH()));
            downAfterA.setText(CommonUtils.isEmptyReturnStr(info.getDownAfterA()));
            downAfterB.setText(CommonUtils.isEmptyReturnStr(info.getDownAfterB()));
            downAfterC.setText(CommonUtils.isEmptyReturnStr(info.getDownAfterC()));
            downAfterD.setText(CommonUtils.isEmptyReturnStr(info.getDownAfterD()));
            downAfterE.setText(CommonUtils.isEmptyReturnStr(info.getDownAfterE()));
            downAfterF.setText(CommonUtils.isEmptyReturnStr(info.getDownAfterF()));
            downAfterG.setText(CommonUtils.isEmptyReturnStr(info.getDownAfterG()));
            downAfterH.setText(CommonUtils.isEmptyReturnStr(info.getDownAfterH()));

        }
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }
}